package com.ks.app.ad.service.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Resource;
import javax.persistence.Query;
import org.springframework.stereotype.Service;
import com.ks.app.admin.hibernate.KsOperaterLog;
import com.ks.app.admin.hibernate.KsProvinceInfo;
import com.ks.app.admin.hibernate.KsProvinceSmsc;
import com.ks.app.admin.hibernate.KsResolution;
import com.ks.app.admin.hibernate.KsVersion;
import com.ks.app.admin.service.KsVersionService;
import com.ks.app.admin.service.KsoperatelogService;
import com.ks.app.admin.service.KsresolutionService;
import com.ks.app.admin.service.ProvinceService;
import com.ks.app.admin.service.impl.BaseServiceImpl;
import com.ks.app.ad.hibernate.KsAd;
import com.ks.app.ad.hibernate.KsAdImage;
import com.ks.app.ad.service.KsadService;
import com.ks.app.admin.util.Pager;
import com.ks.app.search.util.Constants;
import java.io.File;
import java.net.URLEncoder;

@Service("ksadService")
public class KsadServiceImpl extends BaseServiceImpl implements KsadService{
	
	@Resource
	private KsoperatelogService ksoperatelogService;
	
	@Resource
	private KsresolutionService ksresolutionService;
	
	@Resource
	private ProvinceService provinceService;
	
	@Resource
	private KsVersionService ksVersionService;
	
	private KsOperaterLog ksOperateLog;		
	public KsOperaterLog getKsOperateLog() {
		return ksOperateLog;
	}
	public void setKsOperateLog(KsOperaterLog ksOperateLog) {
		this.ksOperateLog = ksOperateLog;
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////ksad
	//根据条件分页展示广告数据
	public List<KsAd> GetKsAdList(String uiid, Pager pager) {
		
		String sql = " from KsAd where 1=1 ";		
		if(uiid!=null&&uiid.length()>0)
		{
			sql=sql+"  and adid=? ";	
		}		
		Query query = em.createQuery(sql+" order by orderid ");
		if(uiid!=null&&uiid.length()>0)
		{   			
			long mid=Long.parseLong(uiid);
			query.setParameter(1, mid);
		}
		query.setMaxResults(pager.getCntOfPage()) ;
	    query.setFirstResult( (pager.getCurPage()-1) * pager.getCntOfPage() );
	    pager.setTotoalCnt(GetcntKsAd(uiid));
		@SuppressWarnings("unchecked")
		List <KsAd> list = query.getResultList();	
		return list;
	}
	
	public List<KsAd> GetKsAdList(String uiid,String adname,String url,String sd,String ed,String isshow, Pager pager)
	{

		String sql = " from KsAd where 1=1 ";		
		if(uiid!=null&&uiid.length()>0)
		{
			sql=sql+"  and adid=? ";	
		}	
		if(adname!=null&&adname.length()>0)
		{
			sql=sql+"  and instr(title,?)>0 ";	
		}	
		if(url!=null&&url.length()>0)
		{
			sql=sql+"  and instr(url,?)>0 ";	
		}	
		if(sd!=null&&sd.length()>0)
		{
			sql=sql+"  and startdate=? ";	
		}	
		if(ed!=null&&ed.length()>0)
		{
			sql=sql+"  and enddate=? ";	
		}
		if(isshow!=null&&isshow.length()>0)
		{
			sql=sql+"  and isshow=? ";	
		}
		Query query = em.createQuery(sql+" order by orderid ");
		int i=0;
		if(uiid!=null&&uiid.length()>0)
		{   	
			i++;
			long mid=Long.parseLong(uiid);
			query.setParameter(i, mid);
		}
		if(adname!=null&&adname.length()>0)
		{
			i++;
			sql=sql+"  and instr(title,?)>0 ";	
			query.setParameter(i, adname);
		}	
		if(url!=null&&url.length()>0)
		{
			i++;
			sql=sql+"  and instr(url,?)>0 ";	
			query.setParameter(i, url);	
		}	
		if(sd!=null&&sd.length()>0)
		{   	
			i++;			
			query.setParameter(i, sd);
		}
		if(ed!=null&&ed.length()>0)
		{   	
			i++;			
			query.setParameter(i, ed);
		}
		if(isshow!=null&&isshow.length()>0)
		{   	
			i++;			
			query.setParameter(i, Long.parseLong(isshow));
		}
		query.setMaxResults(pager.getCntOfPage()) ;
	    query.setFirstResult( (pager.getCurPage()-1) * pager.getCntOfPage() );
	    pager.setTotoalCnt(GetcntKsAd(uiid,adname,url, sd, ed, isshow));
		@SuppressWarnings("unchecked")
		List <KsAd> list = query.getResultList();	
		return list;
	}
	
	
	//根据客户登录名称条件分页展示广告数据 对外客户
	public List<KsAd> GetKsregAdList(String username, Pager pager) {

		String sql = " from KsAd where 1=1 ";		
		if(username!=null&&username.length()>0)
		{
			sql=sql+"  and username=? ";	
		}	
		else
		{
			return null;
		}
		Query query = em.createQuery(sql+" order by orderid ");
		if(username!=null&&username.length()>0)
		{   			
			query.setParameter(1, username);
		}
		query.setMaxResults(pager.getCntOfPage()) ;
	    query.setFirstResult( (pager.getCurPage()-1) * pager.getCntOfPage() );
	    pager.setTotoalCnt(GetcntKsregAd(username));
		@SuppressWarnings("unchecked")
		List <KsAd> list = query.getResultList();	
		return list;
	}
	
	
	//获得记录行数
	public int GetcntKsregAd(String username) {

		String sql = "select count(*) from KsAd where 1=1 ";
		if(username!=null&&username.length()>0)
		{
			sql=sql+" and username=? ";	
		}	
		else
		{
			return 0;
		}
		Query query = em.createQuery(sql);
		if(username!=null&&username.length()>0)
		{   				
			query.setParameter(1, username);
		}
		return Integer.parseInt(String.valueOf(query.getSingleResult()));
	}
	
	
	//获得记录行数
	public int GetcntKsAd(String rid) {
		
		String sql = "select count(*) from KsAd where 1=1 ";
		
		if(rid!=null&&rid.length()>0)
		{
			sql = sql+" and adid="+Long.parseLong(rid);	
		}			
		
		Query query = em.createQuery(sql);
		
		return Integer.parseInt(String.valueOf(query.getSingleResult()));
	}
	
	//获得记录行数
	public int GetcntKsAd(String uiid,String adname,String url,String sd,String ed,String isshow) {

		
		String sql = " select count(*) from KsAd where 1=1 ";		
		if(uiid!=null&&uiid.length()>0)
		{
			sql=sql+"  and adid=? ";	
		}	
		if(adname!=null&&adname.length()>0)
		{
			sql=sql+"  and instr(title,?)>0 ";	
		}	
		if(url!=null&&url.length()>0)
		{
			sql=sql+"  and instr(url,?)>0 ";	
		}	
		if(sd!=null&&sd.length()>0)
		{
			sql=sql+"  and startdate=? ";	
		}	
		if(ed!=null&&ed.length()>0)
		{
			sql=sql+"  and enddate=? ";	
		}
		if(isshow!=null&&isshow.length()>0)
		{
			sql=sql+"  and isshow=? ";	
		}
		Query query = em.createQuery(sql);
		int i=0;
		if(uiid!=null&&uiid.length()>0)
		{   	
			i++;
			long mid=Long.parseLong(uiid);
			query.setParameter(i, mid);
		}
		if(adname!=null&&adname.length()>0)
		{
			i++;
			query.setParameter(i, adname);
		}	
		if(url!=null&&url.length()>0)
		{
			i++;	
			query.setParameter(i, url);	
		}	
		if(sd!=null&&sd.length()>0)
		{   	
			i++;			
			query.setParameter(i, sd);
		}
		if(ed!=null&&ed.length()>0)
		{   	
			i++;			
			query.setParameter(i, ed);
		}
		if(isshow!=null&&isshow.length()>0)
		{   	
			i++;			
			query.setParameter(i, Long.parseLong(isshow));
		}
		return Integer.parseInt(String.valueOf(query.getSingleResult()));
	}
	
	
	//根据id 得到具体的广告
	public List<KsAd> GetKsAd(String uiid) {

		String sql = " from KsAd where 1=1  ";	
		if(uiid!=null&&uiid.length()>0)
		{
			sql=sql+" and adid=? ";	
		}		
		else
		{return null;}
		Query query = em.createQuery(sql+" order by orderid ");
		if(uiid!=null&&uiid.length()>0)
		{   			
			long mid=Long.parseLong(uiid);
			query.setParameter(1, mid);
		}	   
		@SuppressWarnings("unchecked")
		List <KsAd> list = query.getResultList();	
		return list;
	}
	
	
	//根据选择的ID串 生成文件 用于下载 并且将版本号升级一位
	public String ToCreatetxt(String uiid,String username) {
		try {

			String sql = " from KsAd where 1=1  ";	
			if(uiid!=null&&uiid.length()>0)
			{
				sql=sql+" and adid in("+uiid+") ";	
			}	
			else{ return "";}
			Query query = em.createQuery(sql+" order by orderid ");
			
			@SuppressWarnings("unchecked")
			List <KsAd> listad = query.getResultList();//获得要生成文件的所有广告	
			if(listad.size()>0)
			{
				List<KsResolution> lstresol=ksresolutionService.GetKsResolutionList("1");//获得所有的分辨率
				List<KsProvinceInfo> lstpro=provinceService.selectProvince();//获得所有的省份
				if(lstresol==null||lstresol.size()==0||lstpro==null||lstpro.size()==0)
				{
					return "";
				}
				
				for(int r=0;r<lstresol.size();r++)//按分辨率  生成广告描述文件
				{				 
					for(int p=0;p<lstpro.size();p++)//按省份
					 {
							StringBuilder sbadfile=new StringBuilder();
							for(int i=0;i<listad.size();i++)//广告
							{				
								List<KsAdImage> ksimg= GetKsAdimgByAdid(listad.get(i).getAdid());
								for(int k=0;k<ksimg.size();k++)								
									{
										if(ksimg.get(k).getResolution().trim().equals(lstresol.get(r).getResolution().trim()))
										{
											if(listad.get(i).getIsshow()==1)//只有是有效的广告才能添加
											{
												if(listad.get(i).getProvince().indexOf("|"+lstpro.get(p).getPId()+"|")>=0)//开通了此省份 则添加此广告
												{
													String stime=listad.get(i).getStarttime();
													String etime=listad.get(i).getEndtime();
													if(stime.length()==1)
													{
														stime="0"+stime;
													}
													if(etime.length()==1)
													{
														etime="0"+etime;
													}
													String fname=ksimg.get(k).getImagepath();
													
													fname=fname.substring(fname.lastIndexOf("/")+1);
													
													sbadfile.append(listad.get(i).getIsshow()+"#"+listad.get(i).getAdid()+"#"+fname+"#"+listad.get(i).getUrl()+"#"+listad.get(i).getTitle()+"#"+stime+"#"+etime+"\r\n");
												}
											}
										}
									}								
							 }

							if(sbadfile.toString().trim().length()>0)
							{
								try {
									String filepath = Constants.CONS_PROPERTIES.getValue("filedir")+"/Ad/"+lstresol.get(r).getResolution()+"/"+lstpro.get(p).getPId()+"/ads.txt";
									File file;
									try 
									{
										file = checkExist(filepath);
									} 
									catch (Exception e)
									{
								
										e.printStackTrace();
										return "";
									}
																
									FileWriter fileWriter=new FileWriter(file,false);									  
									    fileWriter.write(sbadfile.toString().trim());									    
									   fileWriter.flush();
									   fileWriter.close();
								}
								catch (IOException e) {
						
									e.printStackTrace();
									return "";
								}	
							}
						}
				}
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
				//版本号升级一位
				long getversionId=ksVersionService.getversionId(3, "", 2);
				if(getversionId>=0)
				{
					KsVersion kv= new KsVersion();
					kv.setCreatetime(sdf.format(new Date()));
					kv.setFilepath(uiid);
					kv.setVersion(String.valueOf(getversionId+1));
					kv.setStatus(2);
					kv.setVtype(3);
					ksVersionService.addKsVersion(kv,username);
				}
				else//版本号获取失败
				{
					return "";
				}
				return "Y";
				
			}
			return "";
		} 
		catch (Exception e) {
		
			e.printStackTrace();
			return "";
		}
	}
	/**
     * 判断文件及目录是否存在，若不存在则创建文件及目录
     * @param filepath
     * @return
     * @throws Exception
     */
	public static File checkExist(String filepath) throws Exception{
       File file=new File(filepath);
      
       if (file.exists()) {//判断文件目录的存在
           if(file.isDirectory()){//判断文件的存在性      
             }else{
              file.createNewFile();//创建文件
             }
       }else {
           File file2=new File(file.getParent());
           file2.mkdirs();
           if(file.isDirectory()){      
             }else{      
              file.createNewFile();//创建文件 
             }
       }
       return file;
    } 

	
	//根据选择的ID串 生成字符串 用于下载
	public String GetKsAdFileByIdlist(String uiid) {

		String sql = " from KsAd where 1=1  ";	
		if(uiid!=null&&uiid.length()>0)
		{
			sql=sql+" and adid in("+uiid+") ";	
		}	
		else{ return "";}
		Query query = em.createQuery(sql+" order by orderid ");
		
		@SuppressWarnings("unchecked")
		List <KsAd> list = query.getResultList();	
		if(list.size()>0)
		{
			StringBuilder sbadfile=new StringBuilder();
			for(int i=0;i<list.size();i++)
			{
				if(list.get(i).getIsshow()==1)
				{
					sbadfile.append(list.get(i).getAdid()+"#"+list.get(i).getUrl()+"#"+list.get(i).getTitle()+"\r\n");
				}
			}
			return sbadfile.toString().trim();
		}
		return "";
	}
	
	
	//根据名称获得广告
	public List<KsAd> GetKsAdByUsername(String Username) {

		String sql = " from KsAd  ";		
		if(Username!=null&&Username.length()>0)
		{
			sql=sql+" where title=? ";	
		}		
		else
		{return null;}
		Query query = em.createQuery(sql+" order by orderid ");
		if(Username!=null&&Username.length()>0)
		{   
			query.setParameter(1, Username);
		}	   
		@SuppressWarnings("unchecked")
		List <KsAd> list = query.getResultList();	
		return list;
	}
	
	//根据广告ID 获得其所有图片
	public List<KsAdImage> GetKsAdimgByAdid(long id) {

		String sql = " from KsAdImage  ";		
		if(id>0)
		{
			sql=sql+" where adid=? ";	
		}		
		else
		{return null;}
		Query query = em.createQuery(sql+" order by adimgid ");
		if(id>0)
		{   
			query.setParameter(1, id);
		}	   
		@SuppressWarnings("unchecked")
		List <KsAdImage> list = query.getResultList();	
		return list;
	}
	
	public List<KsAdImage> GetKsAdimgByImgid(long id) {

		String sql = " from KsAdImage  ";		
		if(id>0)
		{
			sql=sql+" where adimgid=? ";	
		}		
		else
		{return null;}
		Query query = em.createQuery(sql+" order by adimgid ");
		if(id>0)
		{   
			query.setParameter(1, id);
		}	   
		@SuppressWarnings("unchecked")
		List <KsAdImage> list = query.getResultList();	
		return list;
	}
	
	
	public List<KsAdImage> GetKsAdimgByAdid_resolution(String id,String resolution)
	{
		String sql = " from KsAdImage where 1=1 ";		
		if(id!=null&&id.trim().length()>0)
		{
			sql=sql+" and adid in("+id+") ";	
		}		
		if(resolution!=null&&resolution.trim().length()>0)
		{
			sql=sql+" and resolution='"+resolution+"'";	
		}	
		Query query = em.createQuery(sql+" order by adimgid ");
		 
		@SuppressWarnings("unchecked")
		List <KsAdImage> list = query.getResultList();	
		return list;
	}
	
	//判断广告是否重复
	public List<KsAd> GetKsAdByUrl(String Url,String Uid) {

		String sql = " from KsAd  where 1=1 ";		
		if(Url!=null&&Url.length()>0)
		{
			sql=sql+" and url=? ";	
		}		
		else 
		{return null;}
		if(Uid!=null&&Uid.length()>0)
		{
			sql=sql+" and adid!='"+Uid+"' ";	
		}	
		Query query = em.createQuery(sql+" order by orderid ");
		if(Url!=null&&Url.length()>0)
		{   
			query.setParameter(1, Url);
		}	   
		@SuppressWarnings("unchecked")
		List <KsAd> list = query.getResultList();	
		return list;
	}

	//添加广告
	public long addKsAd(KsAd ksad) {

		try
		{			
			//添加日志
			em.persist(ksad);			
			return ksad.getAdid();
		}
		catch(Exception ee)
		{  
			ee.printStackTrace();
			return 0;
		}
	}
	
	//添加广告图片
	public long addKsAd(KsAd ksad,List<KsAdImage> adimg,String username) {

		try
		{			
			//添加日志
			String uname=ksad.getTitle();
			String cname=ksad.getUrl();
			//em.persist(ksad);
			long res=ksad.getAdid();	
			if(adimg!=null&&adimg.size()>0)
			{
				for(int i=0;i<adimg.size();i++)
				{
					adimg.get(i).setAdid(res);
					em.persist(adimg.get(i));
				}
			}			
			ksOperateLog =new KsOperaterLog();
			ksOperateLog.setContent(username+"添加了广告:[id="+res+"];[名称="+uname+"];[URL="+cname+"];");
			ksOperateLog.setLogtime(new Date());
			ksOperateLog.setOperater(username);
			ksOperateLog.setLogtype(1);
			ksOperateLog.setStatus(2);
			ksOperateLog.setOpename("添加广告");
			ksoperatelogService.save(ksOperateLog);
			
			return res;
		}
		catch(Exception ee)
		{  
			ee.printStackTrace();
			return 0;
		}
	}
	//修改广告
	public long updateKsAd(KsAd KsUser,List<KsAdImage> lstedit,List<KsAdImage> lstadd,String username) {

		try
		{				
			//添加修改日志
			String uname=KsUser.getTitle();
			String cname=KsUser.getUrl();
			long uid=KsUser.getAdid();			
			em.merge(KsUser);		
			
			if(lstedit!=null&&lstedit.size()>0)
			{
				for(int i=0;i<lstedit.size();i++)
				{
					em.merge(lstedit.get(i));
				}
			}
			if(lstadd!=null&&lstadd.size()>0)
			{
				for(int i=0;i<lstadd.size();i++)
				{
					em.persist(lstadd.get(i));
				}
			}
		
			ksOperateLog =new KsOperaterLog();
			ksOperateLog.setContent(username+"修改了广告:[id="+uid+"];[名称="+uname+"];[URL="+cname+"];");
			ksOperateLog.setLogtime(new Date());
			ksOperateLog.setOperater(username);
			ksOperateLog.setLogtype(1);
			ksOperateLog.setStatus(2);
			ksOperateLog.setOpename("修改广告");
			ksoperatelogService.save(ksOperateLog);
			
			return 1;
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			return 0;
		}
	}
    //删除广告
	public int deleteKsAd(String uiid,String username) {

		try
		{			
			if(uiid!=null&&uiid.length()>0)
			{
				String sql = " from KsAd where adid=? ";	
				Query query = em.createQuery(sql);			
				long m_iid=Long.parseLong(uiid);
				query.setParameter(1,m_iid);			
				@SuppressWarnings("unchecked")
				List <KsAd> list = query.getResultList();		
				if(list.size()>0)
				{					
					String uname=list.get(0).getTitle();
					String cname=list.get(0).getUrl();
					long uid=list.get(0).getAdid();
					
					List <KsAdImage> listi =GetKsAdimgByAdid(uid);
					if(listi.size()>0)
					{
						for(int i=0;i<listi.size();i++)
						{
							em.remove(listi.get(i));
						}
					}
					
					em.remove(list.get(0));	
					
					ksOperateLog =new KsOperaterLog();
					ksOperateLog.setContent(username+"删除了广告:[id="+uid+"];[名称="+uname+"];[URL="+cname+"];");
					ksOperateLog.setLogtime(new Date());
					ksOperateLog.setOperater(username);
					ksOperateLog.setLogtype(1);
					ksOperateLog.setStatus(2);
					ksOperateLog.setOpename("删除广告");
					ksoperatelogService.save(ksOperateLog);
					return 1;
				}
			}	
			return 0;
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			return 0;
		}
	}	
	//删除广告 对外客户
	public int deleteKsregAd(String uiid, String username) {
	
		try
		{			
			if(uiid!=null&&uiid.length()>0)
			{
				String sql = " from KsAd where adid=? and username=? ";	
				Query query = em.createQuery(sql);			
				long m_iid=Long.parseLong(uiid);
				query.setParameter(1,m_iid);	
				query.setParameter(2,username);
				@SuppressWarnings("unchecked")
				List <KsAd> list = query.getResultList();		
				if(list.size()>0)
				{
						
					String uname=list.get(0).getTitle();
					String cname=list.get(0).getUrl();
					long uid=list.get(0).getAdid();
					
					List <KsAdImage> listi =GetKsAdimgByAdid(uid);
					if(listi.size()>0)
					{
						for(int i=0;i<listi.size();i++)
						{
							em.remove(listi.get(i));
						}
					}
					em.remove(list.get(0));
					
					ksOperateLog =new KsOperaterLog();
					ksOperateLog.setContent(username+"删除了广告:[id="+uid+"];[名称="+uname+"];[URL="+cname+"];");
					ksOperateLog.setLogtime(new Date());
					ksOperateLog.setOperater(username);
					ksOperateLog.setLogtype(1);
					ksOperateLog.setStatus(2);
					ksOperateLog.setOpename("删除广告");
					ksoperatelogService.save(ksOperateLog);
					return 1;
				}
			}	
			return 0;
		}
		catch(Exception ee)
		{return 0;}
	}
	
	public String getzipfilepath(String resolution,String smsc,String versionid,String isforce,String dirpath)
	{		
		try {
			
			KsVersion kv=ksVersionService.getversion(3,"",2);
			if(kv==null){return "";}//没有最新的版本记录 无法下载
			String verid=kv.getVersion();			
			String adidlist=kv.getFilepath();
			if(isforce.equals("1")||Long.parseLong(versionid)<Long.parseLong(verid))//强制下载或者传入的版本低于最新版本
			{
				List<KsAdImage> lstimg =GetKsAdimgByAdid_resolution(adidlist,resolution);
				if(lstimg==null||lstimg.size()==0)//没有图片可下载
				{
					return "";
				}
				//根据SMSC获得省份ID 如果省份ID不大于0 不能下载ADS.txt描述文件
				KsProvinceSmsc  kps=provinceService.getpidbysmsc(smsc);
				if(kps==null||kps.getId()<=0)
				{
					return "";
				}
				String pid=kps.getPId();
				int flen=lstimg.size()+1;
				File[] files=new File[12];//最多11个广告+一个描述文件
				 for(int k=0;k<lstimg.size();k++)
				    { 	
					 List<KsAd> kad=GetKsAd(String.valueOf(lstimg.get(k).getAdid()));
					 if(kad!=null&&kad.size()>0)
					 {
						 if(kad.get(0).getProvince().indexOf("|"+pid+"|")>=0)
						 {
							 files[k]=new File(lstimg.get(k).getImagepath().trim()); 
						 }
					 }					  
				    }
				 
				 
				 files[lstimg.size()]= new File(Constants.CONS_PROPERTIES.getValue("filedir")+"/Ad/"+resolution+"/"+pid+"/ads.txt");
				 
				 byte[] bt=new byte[8192]; 
				  String strzipName= Constants.CONS_PROPERTIES.getValue("filedir")+"/Ad/version/"+resolution+"/"+pid+"/"+verid+".zip";
				   try 
				   { 
					   File f=checkExist(strzipName);
					   if(f.length()==0)
					   {
						   FileOutputStream ff=new FileOutputStream(f);
						    ZipOutputStream zout=new ZipOutputStream(ff);//(new FileOutputStream(strzipName)); 
						    //循环下载每个文件并读取内容 
						    for(int j=0;j<files.length;j++)
						    { 
						    	if(files[j]==null)
						    	{
						    		continue;
						    	}
							     FileInputStream is=new FileInputStream(files[j]); 
							     ZipEntry ze=new ZipEntry(URLEncoder.encode(files[j].getName(), "UTF-8"));//设置文件编码格式 
							     zout.putNextEntry(ze); 
							     int len; 
							     //读取下载文件内容 
							     while((len=is.read(bt))>0){ 
							      zout.write(bt, 0, len); 
							     } 
							     zout.closeEntry(); 
							     is.close(); 
						    } 
						    zout.close(); 	
					   }
				   } 
				   catch (FileNotFoundException e)
				   { 
					    e.printStackTrace(); 
					    return "";
				   }
				   catch (IOException e) 
				   { 
					    e.printStackTrace(); 
					    return "";
				   }
				return strzipName;
			}
			return "";
		} 
		catch (Exception e) {
	
			e.printStackTrace();
			return "";
		}
	}
	


}
